package ty.tryagainforever1129.pikachumemory.list;
import java.util.ArrayList;
import ty.tryagainforever1129.pikachumemory.common.Constant;
import ty.tryagainforever1129.pikachumemory.layout.ItemLayout;
import ty.tryagainforever1129.pikachumemory.model.ItemModel;
import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

/** @author tranvanthe play game layout. */
public class ItemLayoutList {
    /** list item layout. */
    private ArrayList<ItemLayout> listItemLayout;
    private TextView textCounter;

    public View createView(final Context context) {
        // init main layout.
        RelativeLayout view = new RelativeLayout(context);
        view.setBackgroundResource(Constant.ITEM_LAYOUT_LIST_DRAWABLE[Constant.COMMON_6]);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        view.setLayoutParams(params);
        
        //get display size.
        int width = (int)(Constant.ITEM_LAYOUT_SCALE * ((Activity)context).getWindowManager().getDefaultDisplay().getWidth());
        int height = (int)(Constant.ITEM_LAYOUT_SCALE * ((Activity)context).getWindowManager().getDefaultDisplay().getHeight());
        
        ItemModel itemModel;
        ItemLayout itemLayout;
        listItemLayout = new ArrayList<ItemLayout>();
        // create item.
        for (int i = 0; i < Constant.ITEM_LAYOUT_LIST_RULE.length; i++) {
            // create left top item.
            itemModel = new ItemModel(width, height, Constant.ITEM_LAYOUT_LIST_RULE[i],
                    Constant.ITEM_LAYOUT_LIST_DRAWABLE[Constant.COMMON_5], Constant.ITEM_LAYOUT_LIST_DRAWABLE[i]);
            // create left top layout.
            itemLayout = new ItemLayout(context);
            // add to view.
            view.addView(itemLayout.createView(itemModel));
            // add to list.
            listItemLayout.add(itemLayout);
        }
        
        // add text counter.
        textCounter = new TextView(context);
        params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);
        textCounter.setTextAppearance(context, Constant.ITEM_LAYOUT_STYLE_TEXT_COUNTER);
        textCounter.setLayoutParams(params);
        textCounter.setGravity(Gravity.BOTTOM);
        view.addView(textCounter);
        
        return view;
    }

    public ArrayList<ItemLayout> getListItemLayout() {
        return listItemLayout;
    }

    public void setListItemLayout(ArrayList<ItemLayout> listItemLayout) {
        this.listItemLayout = listItemLayout;
    }
    
    public ItemLayout getItemLayoutAt(int postion) {
        return listItemLayout.get(postion);
    }

    public TextView getTextCounter() {
        return textCounter;
    }

    public void setTextCounter(TextView textCounter) {
        this.textCounter = textCounter;
    }
    
    public void setCounter(int counter) {
        this.textCounter.setText(String.valueOf(counter));
    }
}
